package com.gaoshi.chapter4;

import java.util.Random;

public class ConstructorTest {
    public static void main(String[] args) {
        EmployeeConstructor[] staff = new EmployeeConstructor[3];

        staff[0] = new EmployeeConstructor("Harry", 40000);
        staff[1] = new EmployeeConstructor(60000);
        staff[2] = new EmployeeConstructor();

        for (EmployeeConstructor e : staff) {
            System.out.println(e.toString());
        }
    }
}

class EmployeeConstructor {
    private static int nextId;

    static {
        Random generator = new Random();
        // set nextId to random number between 0 and 9999
        nextId = generator.nextInt(10000);
    }

    private final int id;
    private String name = "";
    private double salary;

    {
        id = nextId;
        nextId++;
        System.out.println("id=" + id + ", nextId=" + nextId);
    }

    public EmployeeConstructor(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public EmployeeConstructor(double salary) {
        this("Employee #" + nextId, salary);
    }

    public EmployeeConstructor() {
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("EmployeeConstructor{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", salary=").append(salary);
        sb.append('}');
        return sb.toString();
    }
}